<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-01-02
 * Time: 09:55
 */

namespace app\api\controller\v1;

use app\api\model\WxPic;
use app\api\model\WxReply as ReplyModel;
use app\api\model\WxText;
use think\facade\Request;

class Reply
{
    /**
     * 获取信息列表
     */
    public function getList()
    {
        $data = ReplyModel::order('create_time desc')->select()->toArray();
        return $data;
    }

    /**
     * 获取信息详情
     * @validate('IdMustBePositiveInt')
     */
    public function getInfo($id)
    {
        $info = ReplyModel::where('id', '=', $id)->find();
        return $info;
    }

    /**
     * 添加
     * @validate('CheckReply')
     */
    public function createData()
    {
        $post = Request::post();
        $amount = new ReplyModel();
        $amount->allowField(true)->save($post);
        return writeJson(201, [], '添加成功！');
    }

    /**
     * 修改
     * @validate('CheckReplay')
     */
    public function updateData()
    {
        $put = Request::put();
        $id = $put['id'];
        unset($put['id']);
        $amount = new ReplyModel();
        $amount->allowField(true)->save($put, ['id' => $id]);
        return writeJson(202, [], '修改成功！');
    }

    /**
     * 修改
     * @param('ids','待删除的id列表','require|array|min:1')
     */
    public function deleteData()
    {
        $ids = Request::param('ids');
        // 传入多个id组成的数组进行批量删除
        ReplyModel::destroy($ids);
        return writeJson(203, [], '删除成功！');

    }

    /**
     * 获取文字回复内容
     * @validate('IdMustBePositiveInt')
     */
    public function getText($id)
    {
        $info = WxText::where('code','=',$id)->find();
        return $info;
    }

    /**
     * 修改文字回复内容
     * @validate('CheckText')
     */
    public function updateText()
    {
        $post = Request::post();
        $info = WxText::where('code','=',$post['code'])->find();
        if(!$info)
        {
            WxText::create([
                'code' => $post['code'],
                'content' => $post['content']
            ]);
            return writeJson(201, [], '添加成功！');
        }
        else{
            $WxText = new WxText();
            $WxText->allowField(['content'])->save($post, ['id' => $info['id']]);
            return writeJson(202, [], '修改成功！');
        }
    }

    /**
     * 获取图文回复内容
     * @validate('IdMustBePositiveInt')
     */
    public function getPic($id)
    {
        $info = WxPic::with('img')->where('code','=',$id)->find();
        return $info;
    }

    /**
     * 修改图文回复内容
     * @validate('CheckPic')
     */
    public function updatePic()
    {
        $post = Request::post();
        $info = WxPic::where('code','=',$post['code'])->find();
        if(!$info)
        {
            WxPic::create([
                'picurl' => $post['picurl'],
                'img_id' => $post['img_id'],
                'title' => $post['title'],
                'code' => $post['code'],
                'summary' => $post['summary'],
                'url' => $post['url']
            ]);
            return writeJson(201, [], '添加成功！');
        }
        else{
            $WxPic = new WxPic();
            $WxPic->allowField(['picurl','img_id','title','summary','url'])
                ->save($post, ['id' => $info['id']]);
            return writeJson(202, [], '修改成功！');
        }
    }
}